package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //具体逻辑:
        //A. 保存套餐基本信息
        save(setmealDto);
        //B. 获取套餐关联的菜品集合，并为集合中的每一个元素赋值套餐ID(setmealId)
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        //C. 批量保存套餐关联的菜品集合
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public IPage<SetmealDto> pageWithDish(Integer page, Integer pageSize, String name) {
        IPage<SetmealDto> pageInfo = new Page<>(page, pageSize);

        return setmealMapper.getPageByName(pageInfo,name);
    }

    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        //select count(*) from setmeal where id in (1,2,3) and status = 1
        //查询套餐状态，确定是否可用删除
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId, ids);
        lqw.eq(Setmeal::getStatus, 1);
        int setmealCount = count(lqw);
        if (setmealCount > 0) {
            throw new CustomException("有套餐正在售卖,不能删除");
        }

        // 要是删了,连带这套餐和菜品的关系表数据也得删
        //delete from setmeal_dish where setmeal_id in (1,2,3)
        removeByIds(ids);
        LambdaQueryWrapper<SetmealDish> sdLqw = new LambdaQueryWrapper<>();
        sdLqw.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(sdLqw);
    }

    @Override
    @Transactional
    public void updateStatus(Integer type, Long[] ids) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(Setmeal::getId,ids);
        List<Setmeal> list = list(lqw);
        for (Setmeal setmeal : list) {
            if (setmeal.getStatus() == 1){
                setmeal.setStatus(0);
            } else if (setmeal.getStatus() == 0) {
                setmeal.setStatus(1);
            }
            updateById(setmeal);
        }
    }

    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        // 先修改正常的内容
        updateById(setmealDto);
        // 再删除 关系表中的之前内容
        LambdaQueryWrapper<SetmealDish> sdLqw = new LambdaQueryWrapper<>();
        sdLqw.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(sdLqw);
        // 再覆盖新的内容到关系表中
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));
        setmealDishService.saveBatch(setmealDishes);

    }
}
